package com.yoomiito.app.ui.fragment.cicle

import android.os.Bundle
import android.view.View
import com.scrat.app.selectorlibrary.activity.ImagePreviewActivity
import com.scwang.smartrefresh.header.MaterialHeader
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.yoomiito.app.R
import com.yoomiito.app.adapter.cicle.CicleGoodRemmendAdapter
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseLazyFragment
import com.yoomiito.app.model.cicle.CicleRecommendInfo
import com.yoomiito.app.present.cicle.PromotionMaterialPresenter
import com.yoomiito.app.utils.ResUtil
import com.yoomiito.app.widget.SpacesItemDecoration
import com.yoomiito.app.widget.dialog.ShareBottomDialog_2
import com.yoomiito.app.widget.state.ContentType
import com.yoomiito.app.widget.state.EmptyType
import com.yoomiito.app.widget.state.StatusManager
import kotlinx.android.synthetic.main.fm_promotional_material.*
import kotlinx.android.synthetic.main.refresh_rcy_layout.*
import java.util.ArrayList

class PromotionalMaterialFragment : BaseLazyFragment<PromotionMaterialPresenter>() {
    private var mAdapter: CicleGoodRemmendAdapter? = null
    private var mCurrentPage = 1
    private var dataBean: CicleRecommendInfo.DataBean? = null
    private var mShareBottomDialog:ShareBottomDialog_2? = null

    override fun getLayoutId(): Int = R.layout.fm_promotional_material

    override fun newP(): PromotionMaterialPresenter = PromotionMaterialPresenter(App.mAppConfig)

    override fun initData_kotlin(view: View?, savedInstanceState: Bundle?) {
        initStateView()
        initRcy()
        initRefreshLayout()
    }

    private fun initStateView() {
        mStatusManager = StatusManager.Builder(this.context)
                .addType(ContentType(R.layout.refresh_rcy_layout))
                .addType(EmptyType().setEmptyContent("暂未发布内容"))
                .build()
        frameLayout.addView(mStatusManager.view())
    }

    private fun initRefreshLayout() {
        refreshLayout.setRefreshHeader(MaterialHeader(context))
        refreshLayout.setRefreshFooter(ClassicsFooter(context))
        refreshLayout.setOnRefreshListener {
            mCurrentPage = 1
            p.getPromotionalMaterial(mCurrentPage)
        }
        refreshLayout.setOnLoadMoreListener {
            mCurrentPage++
            p.getPromotionalMaterial(mCurrentPage)
        }
        refreshLayout.autoRefresh()
    }

    private fun initRcy() {
        rcy.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
        rcy.addItemDecoration(SpacesItemDecoration(ResUtil.getSize(R.dimen.dp_10)))
        mAdapter = CicleGoodRemmendAdapter(null)
        rcy.adapter = mAdapter
        mAdapter?.setOnItemChildClickListener{ _, view, position ->
            dataBean = mAdapter!!.data[position]
            when (view.id) {
                R.id.tv_share -> share()
                R.id.iv_photo -> ImagePreviewActivity.show(context, 0, dataBean?.content_img as ArrayList<String>, position)
            }
        }
    }

    fun setPublishMaterialData(data: CicleRecommendInfo) {
        mCurrentPage = data.current_page
        if (mCurrentPage == 1) {
            refreshLayout.finishRefresh(true)
            if (data.data == null || data.data.isEmpty()) mStatusManager.showEmpty() else mAdapter?.setNewData(data.data)
        } else {
            refreshLayout.finishLoadMore(true)
            mAdapter?.addData(data.data)
        }
        if (mCurrentPage == data.last_page) refreshLayout.finishLoadMoreWithNoMoreData()
    }

    private fun share() {
        if (mShareBottomDialog==null) mShareBottomDialog = ShareBottomDialog_2(context)
        mShareBottomDialog?.setPicUrls(dataBean?.content_img)
        mShareBottomDialog?.setVideoUrl(null)
        mShareBottomDialog?.setShareContent(dataBean?.content)
        val show = !dataBean?.content_img.isNullOrEmpty()
        mShareBottomDialog?.setShowAlbum(show)
        mShareBottomDialog?.show()
    }
}